package com.whu.waterworks.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;

import com.whu.waterworks.dao.HisWaterPlantDAO;
import com.whu.waterworks.dto.WaterPlantDto;
import com.whu.waterworks.model.TWaterplantdataHis;
import com.whu.waterworks.util.GetObjectName;

public class HisWaterPlantDAOImpl implements HisWaterPlantDAO{


	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	@Override
	public List<TWaterplantdataHis> getAll() {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<TWaterplantdataHis> waterplantdataHis = (List<TWaterplantdataHis>)hibernateTemplate.find("from TWaterplantdataHis _TWaterplantdataHis");
		if(waterplantdataHis!=null && waterplantdataHis.size()>0)
			return waterplantdataHis;
		return null;
	}

	@Override
	public List<TWaterplantdataHis> getWaterplantdatasById(String code) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<TWaterplantdataHis> waterplantdatas = (List<TWaterplantdataHis>)hibernateTemplate.find("from TWaterplantdataHis _TWaterplantdataHis where _TWaterplantdataHis.id.waterPlantNum='"+code+"' order by _TWaterplantdataHis.id.time desc");
		if(waterplantdatas!=null && waterplantdatas.size()>0)
			return waterplantdatas;
		return null;
	}

	@Override
	public List<TWaterplantdataHis> geTWaterplantdataByIdAndOneTime(
			String code, String beginTime) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<TWaterplantdataHis> waterplantdatas = (List<TWaterplantdataHis>)hibernateTemplate.find("from TWaterplantdataHis _TWaterplantdataHis where _TWaterplantdataHis.id.waterPlantNum='"+code+"' and _TWaterplantdataHis.id.time>='"+beginTime+"' order by _TWaterplantdataHis.id.time desc");
		if(waterplantdatas!=null && waterplantdatas.size()>0)
			return waterplantdatas;
		return null;
	}

	@Override
	public List<TWaterplantdataHis> geTWaterplantdataByIdAndTwoTime(
			String code, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<TWaterplantdataHis> waterplantdatas = (List<TWaterplantdataHis>)hibernateTemplate.find("from TWaterplantdataHis _TWaterplantdataHis where _TWaterplantdataHis.id.waterPlantNum='"+code+"' and _TWaterplantdataHis.id.time>='"+beginTime+
				"'and _TWaterplantdataHis.id.time<='"+endTime+"' order by _TWaterplantdataHis.id.time desc");
		if(waterplantdatas!=null && waterplantdatas.size()>0)
			return waterplantdatas;
		return null;
	}

	@Override
	public List<WaterPlantDto> geTWaterplantdataByIdAndOneTime(
			String code, String type, String beginTime) {
		// TODO Auto-generated method stub
		String typeString = "";
		for(String str:GetObjectName.mapsMap.keySet())
		{
			if(GetObjectName.mapsMap.get(str).contains(type))
			{
				typeString = str;
			}
		}
		String queryString = "select new WaterPlantDto(t.id.waterPlantNum,t.id.time,t."+typeString+") from TWaterplantdataHis t where t.id.waterPlantNum='"+code+"' and t.id.time>='"+beginTime+
				"' order by t.id.time desc";
		System.out.println("HisWaterPlantDaoIml typeString="+typeString);
		@SuppressWarnings("unchecked")
		List<Object[]> temp=(List<Object[]>) hibernateTemplate.find(queryString);
		if(temp!=null && temp.size()>0)
		{
			List<WaterPlantDto> waterPlantDtos = new ArrayList<>();
			for(Object[] o:temp)
			{
				WaterPlantDto wDto = new WaterPlantDto();
				wDto.setStationnum((String) o[0]);
				wDto.setTime((String) o[1]);
				wDto.setValue((String) o[2]);
				waterPlantDtos.add(wDto);
			}
			System.out.println("HisWaterPlantDaoIml waterPlantDtos="+waterPlantDtos.size());
			return waterPlantDtos;
		}
		
		return null;
	}

	@Override
	public List<WaterPlantDto> geTWaterplantdataByIdTypeAndTwoTime(
			final String code,final String type,final String beginTime,final String endTime) {
		// TODO Auto-generated method stub
		
			String typeString = "";
				for(String str:GetObjectName.mapsMap.keySet())
				{
					if(GetObjectName.mapsMap.get(str).contains(type))
					{
						typeString = str;
					}
				}
				String queryString = "select t.id.waterPlantNum,t.id.time,t."+typeString+" from TWaterplantdataHis t where t.id.waterPlantNum='"+code+"' and t.id.time>='"+beginTime+
						"'and t.id.time<='"+endTime+"' order by t.id.time desc";
				System.out.println("HisWaterPlantDaoIml typeString="+typeString);
				@SuppressWarnings("unchecked")
				List<Object[]> temp=(List<Object[]>) hibernateTemplate.find(queryString);
				if(temp!=null && temp.size()>0)
				{
					List<WaterPlantDto> waterPlantDtos = new ArrayList<>();
					for(Object[] o:temp)
					{
						WaterPlantDto wDto = new WaterPlantDto();
						wDto.setStationnum((String) o[0]);
						wDto.setTime((String) o[1]);
						wDto.setValue((String) o[2]);
						waterPlantDtos.add(wDto);
					}
					System.out.println("HisWaterPlantDaoIml waterPlantDtos="+waterPlantDtos.size());
					return waterPlantDtos;
				}
				return null;
		
	}

	@Override
	public boolean save(TWaterplantdataHis tWaterplantdataHis) {
		// TODO Auto-generated method stub
		try {
			hibernateTemplate.save(tWaterplantdataHis);
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		
		return true;
	}

}
